package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubInfoDao;
import com.game.server.db.dao.XyClubMemberInfoDao;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.message.proto.in.AbstractXYclubcreateReq;
import com.game.server.message.proto.out.XYclubcreateRsp;
import com.game.server.util.Util;
import net.sf.ezmorph.object.DateMorpher;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

public class XYclubcreateReq extends AbstractXYclubcreateReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        LogManager.getInstance().Log("俱乐部添加");
        if (client != null && client.checkClick(msgcode)) {
            XYclubcreateRsp rsp = new XYclubcreateRsp();
            rsp.setIsok(4);//俱乐部添加失败

            if (this.clubname != null && !"".equals(this.clubname)) {
                UserInfoData userInfoData = DataManager.getInstance().getMapUserData().get(client.getUserId());
                String MaximumNumberOfClubsForUsers = DataManager.getInstance().systemValue("MaximumNumberOfClubsForUsers");
                if (MaximumNumberOfClubsForUsers != null &&
                        Util.isInteger(MaximumNumberOfClubsForUsers) &&
                        userInfoData.getClubids().size() < Integer.parseInt(MaximumNumberOfClubsForUsers)
                ) {
                    String CreateAClubToConsumePoints = DataManager.getInstance().systemValue("CreateAClubToConsumePoints");
                    if (CreateAClubToConsumePoints != null &&
                            Util.isInteger(CreateAClubToConsumePoints) &&
                            client.getVo().getIntegral() >= Integer.parseInt(CreateAClubToConsumePoints)
                    ) {
                        if(this.headurl == null || this.headurl.isEmpty()){
                            this.headurl = client.getVo().getHead();

                        }
                        XyClubInfo clubInfo = new XyClubInfo();
                        clubInfo.setCreatorid(client.getUserId());
                        clubInfo.setName(this.clubname);
                        clubInfo.setHeadurl(this.headurl);
                        clubInfo.setContent(this.content);

                        //加入创建时间
                        Date date = new Date();
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String date1=formatter.format(date);
                        clubInfo.setCreatedate(Timestamp.valueOf(date1));

                        int clubinfoID = XyClubInfoDao.insertInto(clubInfo);
                        if (clubinfoID > 0) {
                            clubInfo = XyClubInfoDao.findById(clubinfoID);
                            if (clubInfo.getId() != 0) {
                                DataManager.getInstance().getMapClub().put(clubInfo.getId(), clubInfo);

                                XyClubMemberInfo player = new XyClubMemberInfo();
                                LogManager.getInstance().Log("俱乐部id:"+clubInfo.getId());
                                player.setCid(clubInfo.getId());
                                player.setUid(client.getUserId());
                                player.setIsManager(1);
                                player.setPid(0);
                                long clubMemberid = XyClubMemberInfoDao.insertInto(player);
                                LogManager.getInstance().Log("俱乐部idclubMemberid:"+String.valueOf(clubMemberid));
                                player = XyClubMemberInfoDao.findById(clubMemberid);
                                clubInfo.addPlayer(player);
                                LogManager.getInstance().Log("俱乐部id:"+clubInfo.getId());
                                //将俱乐部ID添加到缓存中
                                DataManager.getInstance().getMapUserData().get(client.getUserId()).getClubids().add(clubInfo.getId());

                                client.getVo().AddIntegral(-Integer.parseInt(CreateAClubToConsumePoints));
                                rsp.setIsok(0);//俱乐部添加成功
                                rsp.setClubid(clubInfo.getId());

                            }
                        }
                    } else {
                        LogManager.getInstance().Log("用户俱乐部已达上限");
                        rsp.setIsok(1);//用户俱乐部已达上限
                    }
                } else {
                    LogManager.getInstance().Log("用户俱乐部已达上限");
                    rsp.setIsok(3);//用户俱乐部已达上限
                }
            } else {
                rsp.setIsok(2);//名字格式不对
            }
            client.Send(rsp);
        }
    }

}
